//----------------------------------------------------------------------------
// Filename                : message_protocol_application.c
//
// Functional Description  : This file contains the overridden functions for the common
//                           messaging protocol. This allows the common messaging component
// 							 to work while allowing application extensions to the protocol
//							 in a common way. 
//-----------------------------------------------------------------------------

#include "message_protocol_application.h"
#include "afe_data.h"

/*
 *  \fn unsigned int message_handler_application_read()
 *  \brief Callback function from common message handler for reading application data.
 * 
 *  Returns non-zero pointer to data to read from application, or zero if the command ID is invalid
 */

unsigned int message_handler_application_read()
{
	unsigned int pData;
	unsigned char cmd = uart_rx_buffer[0];
	
	switch (cmd)
	{
		case CmdAdminState:
			pData = (unsigned int)&AppAdminState;
			break;
		case CmdHwOc:
			pData = (unsigned int)&AppHwOc;
			break;
		case CmdDeadTime:
			pData = (unsigned int)&AppDeadTime;
			break;
		case CmdThrottleOff:
			pData = (unsigned int)&AppOffThrottle;
			break;
		case CmdIfilterMax:
			pData = (unsigned int)&AppIfilterMax;
			break;
		case CmdIfilterMid:
			pData = (unsigned int)&AppIfilterMid;
			break;
		case CmdImaxNonComplementaryPwm:
			pData = (unsigned int)&AppImaxNonCmpl;
			break;
		case CmdINonComplementaryPwmHysteresis:
			pData = (unsigned int)&AppImaxNonCmplHys;
			break;
		case CmdThrottleMinVoltage:
			pData = (unsigned int)&AppThrottleMinVoltage;
			break;
		case CmdPwmLowUpperBound:
			pData = (unsigned int)&AppPwmLowUpperBound;
			break;
		case CmdPwmMidUpperBound:
			pData = (unsigned int)&AppPwmMidUpperBound;
			break;
		case CmdPwmMinDuty:
			pData = (unsigned int)&AppPwmMinDuty;
			break;
		case CmdStatus:
			pData = (unsigned int)&AppStatus;
			break;
		case CmdSpeed:
			pData = (unsigned int)&AppSpeed;
			break;
		case CmdThrottleVoltage:
			pData = (unsigned int)&AppThrottleVoltage;
			break;
		case CmdThrottleVoltageFiltered:
			pData = (unsigned int)&AppThrottleVoltageFiltered;
			break;
		case CmdCurrent:
			pData = (unsigned int)&AppCurrent;
			break;
		case CmdDutyRange:
			pData = (unsigned int)&AppDutyRange;
			break;
		case CmdPwmDuty:
			pData = (unsigned int)&AppPwmDuty;
			break;
		case CmdMotorState:
			pData = (unsigned int)&AppMotorState;
			break;
		case CmdFanOff:
			pData = (unsigned int)&AppFanOff;
			break;
		default:
			pData = 0xFFFF;
	}
	
	return pData;
}

/*
 *  \fn unsigned int message_handler_application_write(unsigned int data)
 *  \brief Callback function from common message handler for writing application data
 * 
 *  Returns nonzero value on success and zero on invalid command or data
 */

unsigned int message_handler_application_write(unsigned int data)
{
	unsigned char cmd = uart_rx_buffer[0];
	
	switch (cmd)
	{
		case CmdAdminState:
			*((unsigned int *)&AppAdminState) = data;
			break;
		case CmdHwOc:
			*((unsigned int *)&AppHwOc) = data;
			AfeRegDac1 = (unsigned char)(data >> 8);
			AfeRegDac2 = (unsigned char)(data & 0xFF);
			drv922xxSpiWrite(SPI_DAC1_READ, AfeRegDac1);
			drv922xxSpiWrite(SPI_DAC2_READ, AfeRegDac2);
			break;
		case CmdDeadTime:
			*((unsigned int *)&AppDeadTime) = data;
			break;
		case CmdThrottleOff:
			*((unsigned int *)&AppOffThrottle) = data;
			break;
		case CmdIfilterMax:
			*((unsigned int *)&AppIfilterMax) = data;
			break;
		case CmdIfilterMid:
			*((unsigned int *)&AppIfilterMid) = data;
			break;
		case CmdImaxNonComplementaryPwm:
			*((unsigned int *)&AppImaxNonCmpl) = data;
			break;
		case CmdINonComplementaryPwmHysteresis:
			*((unsigned int *)&AppImaxNonCmplHys) = data;
			break;
		case CmdThrottleMinVoltage:
			*((unsigned int *)&AppThrottleMinVoltage) = data;
			break;
		case CmdPwmLowUpperBound:
			*((unsigned int *)&AppPwmLowUpperBound) = data;
			break;
		case CmdPwmMidUpperBound:
			*((unsigned int *)&AppPwmMidUpperBound) = data;
			break;
		case CmdPwmMinDuty:
			*((unsigned int *)&AppPwmMinDuty) = data;
			break;
		case CmdThrottleVoltage:
			*((unsigned int *)&AppThrottleVoltage_Reference) = data;
			response_buffer[2] = (*((unsigned int *)&AppThrottleVoltageFiltered) >> 8);
			response_buffer[3] = (*((unsigned int *)&AppThrottleVoltageFiltered) & 0xFF);
			break;
		case CmdFanOff:
			*((unsigned int *)&AppFanOff) = data;
			if (AppFanOff == 1) {
				AppThrottleVoltage = 0;
				AppThrottleVoltageFiltered = 0;
			}
		//	AppFanOff = data;
			break;
		case cmdMsg_fan_dir_cw:
		//	reverse_drive = 1;
		//	if (target_drive_direction == DIR_STOP)
				target_drive_direction = DIR_REVERSE;
			break;
		case cmdMsg_fan_dir_ccw:
		//	reverse_drive = 0;
		//	if (target_drive_direction == DIR_STOP)
				target_drive_direction = DIR_FORWARD;
			break;
		case cmdMsg_fan_dir_change:
//			if (reverse_drive == 1)
//				reverse_drive = 0;
//			else
//				reverse_drive = 1;
			if (target_drive_direction == DIR_FORWARD)
				target_drive_direction = DIR_REVERSE;
			else if (target_drive_direction == DIR_REVERSE)
				target_drive_direction = DIR_FORWARD;
			break;
		default:
			return 0xFFFF;
	}
	
	return 1;
}
